package net.cjw.initsb.controller.background;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.common.ServiceResult.ServiceResultMsg;
import net.cjw.initsb.entity.Role;
import net.cjw.initsb.entity.RolePermissionConnect;
import net.cjw.initsb.service.RoleServiceI;

@Api(description = "角色")
@RestController
@RequestMapping("role")
public class RoleController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RoleServiceI roleServiceI;

	@RequiresRoles({"admin"})
	@PostMapping("v{version}/create")
	public ServiceResult<String> create(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "角色名称", defaultValue = "我是角色名称") @RequestParam String role_name,
			@ApiParam(value = "角色标识值", defaultValue = "test") @RequestParam String role_value,
			@ApiParam(value = "角色绑定的权限id,多个逗号隔开", defaultValue = "044c6763819544cda2b8f33039aa8b0f,542b05482f3e4ec3aa996a3a804cbe91") @RequestParam(required = false) String permission_ids) {
		
		if (Strings.isNullOrEmpty(role_name) || Strings.isNullOrEmpty(role_value)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		logger.info("permission_ids: " + permission_ids);
		if (version == 1) {
			Role role = new Role();
			role.setRoleName(role_name);
			role.setRoleValue(role_value);
			return roleServiceI.create(role, permission_ids);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@GetMapping("v{version}/find_role_permission")
	public ServiceResult<List<RolePermissionConnect>> find_role_permission(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "角色id", defaultValue = "") @RequestParam String id) {
		if (Strings.isNullOrEmpty(id)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			return roleServiceI.findRolePermission(id);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}
	@RequiresRoles("admin")
	@PostMapping("v{version}/update")
	public ServiceResult<String> update(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "角色id", defaultValue = "") @RequestParam String id,
			@ApiParam(defaultValue = "我是新角色名") @RequestParam String role_name,
			@ApiParam(defaultValue = "我是新角色标识值") @RequestParam String role_value,
			@ApiParam(value = "角色绑定的权限id,多个逗号隔开", defaultValue = "044c6763819544cda2b8f33039aa8b0f,542b05482f3e4ec3aa996a3a804cbe91") @RequestParam(required = false) String permission_ids) {

		if (Strings.isNullOrEmpty(id)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Role role = new Role();
			role.setId(id);
			role.setRoleName(role_name);
			role.setRoleValue(role_value);
			return roleServiceI.update(role, permission_ids);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@GetMapping("v{version}/select_all")
	public ServiceResult<Object> selectAll(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "页数", defaultValue = "1") @RequestParam Integer page_num,
			@ApiParam(value = "数据库最大结果数", defaultValue = "10") @RequestParam Integer max_result) {
		if (page_num == null || max_result == null) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			return roleServiceI.selectAll(page_num, max_result);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}
	@RequiresRoles("admin")
	@PostMapping("v{version}/delete")
	public ServiceResult<String> delete(@ApiParam(defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "角色id", defaultValue = "") @RequestParam String id) {
		if (Strings.isNullOrEmpty(id)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			Role role = new Role();
			role.setId(id);
			return roleServiceI.delete(role);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

}
